﻿function layoutHeight(left, right) {
    var left_height = $(left).height();
    var right_height = $(right).height();

    if (left_height > right_height) {
        $(right).height(left_height);
    }
    else {
        $(left).height(right_height);
    }

}
function sideLayoutHeight(side, main, adjust_target) {
    var side_height = $(side).height();
    var main_height = $(main).height();

    if (side_height < main_height) {
        var products_height = $(adjust_target).height();
        $(adjust_target).height(main_height - side_height + products_height);
    }
}




/*---------------menu jquery plugin--------------*/
(function ($) {
    $.fn.main_nav = function (options) {
        var defaults = {
            focus_style_selector: ".main-nav-item-focus-style",
            current_item_selector: ".main-nav-item-current",
            item_selector: ".main-nav-item"
        };

        $.extend(defaults, options);

        var current_item = this.find(defaults.current_item_selector);

        var items = this.find(defaults.item_selector);

        var hover_item = null;

        var focus_obj = this.find(defaults.focus_style_selector); //.height(current_item.innerHeight()).width(current_item.innerWidth());

        var fn_moving = function (item) {
            focus_obj.stop(true);
            focus_obj.animate({ "left": item.position().left, "width": item.innerWidth() });
        };

        items.mouseenter(function () {
            hover_item = $(this);
            fn_moving(hover_item);
        });
        this.mouseleave(function () {
            hover_item = current_item;
            fn_moving(hover_item);
        });

        fn_moving(current_item);
    };

})(jQuery);



/*---------------roller jquery plugin--------------*/
(function ($) {
    $.fn.roller = function (options) {
        var defaults = {
            roller_container_selector: ".roller_container",
            speed: 20000
        };

        $.extend(defaults, options);

        var roller = this;
        var roller_container = roller.find(defaults.roller_container_selector);
        var speed = defaults.speed;

        var reset = function () {
            roller_container.css("left", roller.innerWidth());
        }

        var cal_speed = function () {
            //总移动像素
            var total_move_px = (roller.innerWidth() + roller_container.innerWidth());
            //每毫秒移动像素
            var move_speed = total_move_px / speed;
            //已移动像素
            var moved_px = (roller_container.innerWidth() + roller_container.offset().left - roller.offset().left);
            //还需移动像素
            var need_move_px = total_move_px - moved_px
            return speed - (need_move_px / move_speed);
        }

        var rolling = function (roll_speed) {
            roller_container.animate({ "left": -roller_container.innerWidth() }, roll_speed, "linear", function () {
                reset();
                rolling(speed);
            });
        };

        this.mouseenter(function () {
            roller_container.stop();
        });

        this.mouseleave(function () {
            rolling(cal_speed());
        });

        reset();
        rolling(speed);
    };

})(jQuery);